package com.mobialia.wikiplaces;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class WikiplacesOverlay extends Overlay {

	WikiplacesData wikiplacesData;
	WikiplacesMapActivity activity;
		
	public WikiplacesOverlay(WikiplacesMapActivity activity) {
		super();
		wikiplacesData = WikiplacesData.getInstance(activity);
		this.activity = activity;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {

		Projection projection = mapView.getProjection();

		if (shadow == false && wikiplacesData.getLocationPoint() != null) {

			for (int i = 0; i < wikiplacesData.getSize(); i++) {
				if (i != wikiplacesData.getSelectedPointIndex()) {
					GeoPoint geoPoint = wikiplacesData.getPointToDraw(i);
					Point point = new Point();

					projection.toPixels(geoPoint, point);
					if (point.x > -100 && point.x < canvas.getWidth() + 100
							&& point.y > -100
							&& point.y < canvas.getHeight() + 100)
						drawMarker(i, point, canvas);
				}
			}
			for (int i = 0; i < wikiplacesData.getSize(); i++) {
				if (i != wikiplacesData.getSelectedPointIndex()) {
					GeoPoint geoPoint = wikiplacesData.getPointToDraw(i);
					Point point = new Point();
					projection.toPixels(geoPoint, point);
					if (point.x > -100 && point.x < canvas.getWidth() + 100
							&& point.y > -100
							&& point.y < canvas.getHeight() + 100)
						drawText(i, point, canvas, false);
				}
			}
		}

		if (wikiplacesData.getSelectedPointIndex() != -1) {
			GeoPoint geoPoint = wikiplacesData.getSelectedPoint();
			if (geoPoint == null)
				return;
			Point point = new Point();
			projection.toPixels(geoPoint, point);
			if (point.x > -50 && point.x < canvas.getWidth() + 50
					&& point.y > -50 && point.y < canvas.getHeight() + 50)
				drawMarker(wikiplacesData.getSelectedPointIndex(), point, canvas);
			drawText(wikiplacesData.getSelectedPointIndex(), point, canvas, true);
		}

		super.draw(canvas, mapView, shadow);
	}

	private void drawMarker(int index, Point point, Canvas canvas) {
		Drawable marker = activity.getResources().getDrawable(R.drawable.wikiplace);
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		canvas.drawLine(point.x, point.y - dipToPixels(activity, 46), point.x, point.y, paint);
		marker.setBounds(point.x, point.y - dipToPixels(activity, 48), point.x + dipToPixels(activity, 32), point.y - dipToPixels(activity, 16));
		marker.draw(canvas);
	}

	private void drawText(int index, Point point, Canvas canvas, boolean isSelected) {
		Paint paint = new Paint();
		paint.setFlags(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
		paint.setTypeface(Typeface.DEFAULT_BOLD);
		if (isSelected) {
			paint.setTextSize(dipToPixels(activity, 15));
		} else {
			paint.setTextSize(dipToPixels(activity, 12));
		}

		String text = wikiplacesData.getWikiplace(index).getTitle();

		Rect rect = new Rect();
		paint.getTextBounds(text, 0, text.length(), rect);
		paint.setColor(Color.WHITE);
		canvas.drawRect(point.x + dipToPixels(activity, 5), point.y - (rect.bottom - rect.top),
				point.x + dipToPixels(activity, 5) + (rect.right - rect.left), point.y + 1, paint);
		paint.setColor(Color.BLACK);
		canvas.drawText(text, point.x + dipToPixels(activity, 5), point.y, paint);
	}

	// Busca el lugar más cercana clickada
	@Override
	public boolean onTap(GeoPoint point, MapView mapView) {
		
		// Al doble tap abre el detalle
		int newSelectedPointIndex = wikiplacesData.getNearestIndex(point);
		if (newSelectedPointIndex != wikiplacesData.getSelectedPointIndex()) {			
			Toast toast = Toast.makeText(activity, R.string.tap_again_details, Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.TOP, 0, 0);
			toast.show();
		} else {
			// Lanza intent de detalles
			Intent intent = new Intent(activity, WikiplacesDetailActivity.class);
			Bundle bundle = new Bundle();
			bundle.putInt("selectedPlace", newSelectedPointIndex);
			bundle.putBoolean("calledFromMap", true);
			intent.putExtras(bundle);
			activity.startActivity(intent);
			return true;
		}
		
		wikiplacesData.setSelectedPointIndex(newSelectedPointIndex);
		if (wikiplacesData.getSelectedPointIndex() != -1) {
			GeoPoint geoPoint = wikiplacesData.getSelectedPoint();
			MapController mapController = mapView.getController();
			mapController.animateTo(geoPoint);
		}
		
		int i = wikiplacesData.getSelectedPointIndex();
		if (i == -1) return false;
		return false;
	}
	
	private int dipToPixels(Context c, float dipValue) {  
		return (int) (dipValue * c.getResources().getDisplayMetrics().density + 0.5f);  
	}  
}